package com.tebiecloud.retail.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.constants.BaseConstants;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.retail.client.model.entity.RetailOrder;
import com.tebiecloud.retail.server.service.RetailOrderService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 分销的订单 前端控制器
 * @author tebie
 * @date 2019-09-17
 */
@RestController
@RequestMapping("/retailorder")
public class RetailOrderController {

    @Autowired
    private RetailOrderService retailOrderService;


    /**
     * 个人分享记录
     * @return
     */
    @PostMapping(value = "listpersonal")
    public ResultBody<IPage<RetailOrder>> listpersonal() {
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        String openid = "";
        if(securityUserDetails.getAccountType().equals(BaseConstants.ACCOUNT_TYPE_MP)){
            openid = securityUserDetails.getUsername().split(":")[1];
        }
        return ResultBodyUtils.ok(retailOrderService.selectRetailList(securityUserDetails.getUserId(),openid));
    }







    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<RetailOrder>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        RetailOrder query = pageParams.mapToObject(RetailOrder.class);
        QueryWrapper<RetailOrder> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(retailOrderService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<RetailOrder> get(@RequestParam("id") Long id) {
        RetailOrder entity = retailOrderService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param orderId 订单id,
     * @param userId 用户id,
     * @param poneUserId 父一级userid,
     * @param ptwoUserId 父二级user_id,
     * @param pthreeUserId 父三级userid,
     * @param status 订单状态，0已取消1待付款2待分销4已分销6已退款
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "orderId", required = false) Long orderId,
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "poneUserId", required = false) Long poneUserId,
        @RequestParam(value = "ptwoUserId", required = false) Long ptwoUserId,
        @RequestParam(value = "pthreeUserId", required = false) Long pthreeUserId,
        @RequestParam(value = "status", required = false) Integer status
    ) {
        RetailOrder entity = new RetailOrder();
        entity.setOrderId(orderId);
        entity.setUserId(userId);
        entity.setPoneUserId(poneUserId);
        entity.setPtwoUserId(ptwoUserId);
        entity.setPthreeUserId(pthreeUserId);
        entity.setStatus(status);
        retailOrderService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param orderId 订单id
     * @param userId 用户id
     * @param poneUserId 父一级userid
     * @param ptwoUserId 父二级user_id
     * @param pthreeUserId 父三级userid
     * @param status 订单状态，0已取消1待付款2待分销4已分销6已退款
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "retailOrderId", required = false) Long retailOrderId,
            @RequestParam(value = "orderId", required = false) Long orderId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "poneUserId", required = false) Long poneUserId,
            @RequestParam(value = "ptwoUserId", required = false) Long ptwoUserId,
            @RequestParam(value = "pthreeUserId", required = false) Long pthreeUserId,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        RetailOrder entity = new RetailOrder();
        entity.setRetailOrderId(retailOrderId);
        entity.setOrderId(orderId);
        entity.setUserId(userId);
        entity.setPoneUserId(poneUserId);
        entity.setPtwoUserId(ptwoUserId);
        entity.setPthreeUserId(pthreeUserId);
        entity.setStatus(status);
        retailOrderService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            retailOrderService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            retailOrderService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
